package com.zxy.ziems.server.statistics.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxy.btp.common.response.JsonResponse;
import com.zxy.btp.web.util.TokenUtils;
import com.zxy.ziems.server.statistics.pojo.dto.ExhaustGasChemicalsDto;
import com.zxy.ziems.server.statistics.pojo.dto.GasConsumptionDto;
import com.zxy.ziems.server.statistics.pojo.entity.ExhaustGasChemicalsData;
import com.zxy.ziems.server.statistics.pojo.entity.GasConsumptionData;
import com.zxy.ziems.server.statistics.pojo.param.ExhausGasChemicalQueryParam;
import com.zxy.ziems.server.statistics.pojo.param.GasConsumptionQueryParam;
import com.zxy.ziems.server.statistics.pojo.vo.ValueLabelVo;
import com.zxy.ziems.server.statistics.service.ExhausGasChemicalService;
import com.zxy.ziems.server.statistics.service.GasConsumptionService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URLEncoder;
import java.util.Map;

/**
 * 天然气每日用量记录
 */
@RestController
@RequestMapping("/api/gas/consumption")
public class GasConsumptionController {

    @Autowired
    private GasConsumptionService gasConsumptionService;

    /**
     * 表头
     *
     * @param parentClassifyId
     * @return
     */
    @GetMapping("/header")
    public JsonResponse<Map<String, ValueLabelVo>>  header(@RequestParam String parentClassifyId) {
        Map<String, ValueLabelVo> res = gasConsumptionService.buildHeader(parentClassifyId);
        return JsonResponse.SUCCESS(res);
    }

    /**
     * 新增
     *
     * @param gasConsumptionDto
     * @return
     */
    @PostMapping("/addOrUpdate")
    public JsonResponse<String> addOrUpdate(@RequestBody GasConsumptionDto gasConsumptionDto) {
        String userId = TokenUtils.getUserIdByToken();
        gasConsumptionService.addOrUpdate(gasConsumptionDto,userId);
        return JsonResponse.SUCCESS();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public JsonResponse<String> delete(@RequestParam String id) throws Exception {
        String userId = TokenUtils.getUserIdByToken();
        gasConsumptionService.delete(id);
        return JsonResponse.SUCCESS();
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @PostMapping("/selectPage")
    public JsonResponse<Page<GasConsumptionData>> selectPage(@RequestBody GasConsumptionQueryParam param) {
        Page<GasConsumptionData> res = gasConsumptionService.selectPage(param);
        return JsonResponse.SUCCESS(res);
    }

    /**
     * 导入
     *
     * @param file
     * @return
     */
    @PostMapping("/import/excel")
    public JsonResponse<String> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        String userId = TokenUtils.getUserIdByToken();
        gasConsumptionService.importExcel(file,userId);
        return JsonResponse.SUCCESS();
    }

    /**
     * 导出模板
     */
    @GetMapping("/export/template")
    public void exportTemplate(HttpServletResponse response) throws Exception {
        response.setCharacterEncoding("utf-8");
        String fileName = "天然气每日用量统计";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setCharacterEncoding("utf-8");
        gasConsumptionService.exportTemplate(response);
    }

}
